<%@ include file="/html/common/init.jsp"%>

<script type="text/javascript" src="/js/jquery/jquery.js"></script>

<%
	String categoryId =(String)request.getParameter("categoryId");
	String redirect = ParamUtil.getString(renderRequest, "redirect");
	boolean fieldsEditingDisabled = false;
%>

<liferay-portlet:actionURL portletConfiguration="true"
	name="updateProcess" var="configurationURL" />

<aui:form action="<%= configurationURL %>" method="post" name="fm">
	<aui:input name="<%= Constants.CMD %>" type="hidden"
		value="<%= Constants.UPDATE %>" />
	<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
	<aui:input name="categoryId" type="hidden" value="<%= categoryId %>" />
	<%@ include file="/html/category/common_edit.jsp"%>

	<liferay-ui:panel-container id="webFormConfiguration"
		extended="<%= Boolean.TRUE %>" persistState="<%= true %>">
		<liferay-ui:panel id='webFormFields'
			title='<%= LanguageUtil.get(pageContext, "form-fields") %>'
			collapsible="<%= true %>" persistState="<%= true %>"
			extended="<%= true %>">
			<aui:fieldset cssClass="rows-container webFields">

				<aui:input name="updateFields" type="hidden"
					value="<%= !fieldsEditingDisabled %>" />

				<%
					String formFieldsIndexesParam = ParamUtil.getString(renderRequest, "formFieldsIndexes") ;
				
					List<CustomField>  customFields= null;
					try{
						 customFields = (List)CustomFieldLocalServiceUtil.findByCategoryId(Long.parseLong(categoryId));
						 int i = 1;
						 for(CustomField customfield: customFields) {
							 formFieldsIndexesParam += customfield.getFieldOrder()+((i<customFields.size())? ",":"");
							 i++;
						 }
					}catch(Exception e){}
					
					int[] formFieldsIndexes = null;
	
					if (Validator.isNotNull(formFieldsIndexesParam)) {
						formFieldsIndexes = StringUtil.split(formFieldsIndexesParam, 0);
					}
					else {
						formFieldsIndexes = new int[0];
	
						for (int i = 1; true; i++) {
							String fieldLabel = PrefsParamUtil.getString(preferences, request, "fieldLabel" + i);
	
							if (Validator.isNull(fieldLabel)) {
								break;
							}
	
							formFieldsIndexes = ArrayUtil.append(formFieldsIndexes, i);
						}
	
						if (formFieldsIndexes.length == 0) {
							formFieldsIndexes = ArrayUtil.append(formFieldsIndexes, -1);
						}
					}
	
					int index = 1;
					for (int formFieldsIndex : formFieldsIndexes) {
						request.setAttribute("configuration.jsp-index", String.valueOf(index));
						request.setAttribute("configuration.jsp-formFieldsindex", String.valueOf(formFieldsIndex));
						request.setAttribute("configuration.jsp-fieldsEditingDisabled", String.valueOf(fieldsEditingDisabled));
					%>
				<div class="lfr-form-row"
					id="<portlet:namespace/>fieldset<%= formFieldsIndex %>">
				<div class="row-fields"><jsp:include page="edit_field.jsp" />
				</div>
				</div>

				<%
						index++;
					}
					%>

				<input type="hidden" id="indexId" name="index" value="<%=index %>" />
			</aui:fieldset>
		</liferay-ui:panel>
	</liferay-ui:panel-container>
	<aui:button-row>
		<aui:button type="submit" onClick="getIndex();" />
		<aui:button type="button" value="Back" onClick="history.go(-1)" />
	</aui:button-row>
</aui:form>

<aui:script>
	function getIndex() {
	
		var length = jQuery('.lfr-form-row').length;
		document.getElementById("indexId").value = length;
	}
</aui:script>

<aui:script use="liferay-auto-fields">
	var toggleOptions = function(event) {
	
		var select = this;

		var formRow = select.ancestor('.lfr-form-row');
		var value = select.val();

		var optionsDiv = formRow.one('.options');

		if (value == 'options' || value == 'radio') {
			optionsDiv.all('label').show();
			optionsDiv.show();
		}
		else if (value == 'paragraph') {

			// Show just the text field and not the labels since there
			// are multiple choice inputs

			optionsDiv.all('label').hide();
			optionsDiv.show();
		}
		else {
			optionsDiv.hide();
		}

		var optionalControl = formRow.one('.optional-control');
		var labelName = formRow.one('.label-name');

		if (value == 'paragraph') {
			var inputName = labelName.one('input');

			inputName.val('<liferay-ui:message key="paragraph" />');
			inputName.fire('change');

			labelName.hide();
			optionalControl.hide();

			optionalControl.all('input[type="checkbox"]').attr('checked', 'true');
			optionalControl.all('input[type="hidden"]').attr('value', 'true');
		}
		else {
			optionalControl.show();
			labelName.show();
		}
	};

	var toggleValidationOptions = function(event) {
	
		this.next().toggle();
	};

	var webFields = A.one('.webFields');

	webFields.all('select').each(toggleOptions);

	<c:if test="<%= !fieldsEditingDisabled %>">
		A.delegate('change', toggleOptions, webFields, 'select');
		A.delegate('click', toggleValidationOptions, webFields, '.validation-link');

		A.delegate(
			'change',
			function(event) {
			
				var input = event.currentTarget;
				var row = input.ancestor('.field-row');
				var label = row.one('.field-title');

				if (label) {
					label.html(input.get('value'));
				}
			},
			webFields,
			'.label-name input'
		);

		new Liferay.AutoFields(
			{
				contentBox: webFields,
				fieldIndexes: '<portlet:namespace />formFieldsIndexes',
				sortable: true,
				sortableHandle: '.field-label'
			}
		).render();
	</c:if>
</aui:script>
